/***********************************************
 @Name :        小铭同学
 @Time :        2025-08-15
 @Email :       LiaoMingWJ233@163.com
 @Comment :     功能窗口 智能家居
 @FileName :    widget_funcc_smart_home.cpp
 @Version :     1.0
************************************************/

#include "widget_funcc_smart_home.h"
#include "ui_widget_funcc_smart_home.h"

Widget_funcC_Smart_Home::Widget_funcC_Smart_Home(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget_funcC_Smart_Home)
{
    ui->setupUi(this);   
    
    
    // 音乐播放
    // 字体设置
    // 歌名
    ui->label_music_singer->setFont(QFont("楷体", 12));
    
    // 使用样式表设置渐变颜色
    ui->label_music_singer->setStyleSheet(
                "QLabel {"
                "   color: #E0115F;"
                "   background-color: transparent;"
                "}"
    );
    
    // 歌手
    ui->label_music_song_name->setFont(QFont("楷体", 12));
    
    // 使用样式表设置渐变颜色
    ui->label_music_song_name->setStyleSheet(
                "QLabel {"
                "   color: #E0115F;"
                "   background-color: transparent;"
                "}"
    );
    
    // 歌词
    ui->label_music_lyric->setFont(QFont("楷体", 14));
    
    // 使用样式表设置渐变颜色
    ui->label_music_lyric->setStyleSheet(
                "QLabel {"
                "   color: #E0115F;"
                "   background-color: transparent;"
                "}"
    );
    
    // 初始化定时器
    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &Widget_funcC_Smart_Home::RegularExpression);
    timer->start(1000);  // 1000ms = 1秒触发一次
    
    // 设置 GIF 循环播放
    movie->setCacheMode(QMovie::CacheAll); // 缓存所有帧，提高性能
    movie->setSpeed(100); // 播放速度百分比（100 是正常速度）
    
    ui->label_music->setMovie(movie);//widget_music
    
    // 连接信号和槽
    connect(ui->pushButton_camera, &QPushButton::clicked, this, &Widget_funcC_Smart_Home::togglecamera);
    
    // 音乐
    // 监听播放状态变化
    connect(player, &QMediaPlayer::stateChanged, this, [this](QMediaPlayer::State state) {
        if(state==QMediaPlayer::StoppedState)//  播放器已停止（未播放）
        {
            movie->stop();// 停止播放
            ui->horizontalSlider_music->setValue(0);// 更新滑块位置
        }        
        //qDebug() << "播放状态变化：" << state;
    });
    
    // 设置滑块的范围
    connect(player, &QMediaPlayer::durationChanged, this, [this](qint64 duration) {
        ui->horizontalSlider_music->setRange(0, duration);
        ui->horizontalSlider_music->setSingleStep(duration / 100);  // 可选：设置步长
        ui->horizontalSlider_music->setPageStep(duration / 10);    // 可选：设置页步长
        //qDebug() << "duration:" << duration;           
    });
    
    // 播放进度
    connect(player, &QMediaPlayer::positionChanged, this, [this](qint64 pos){
        //qDebug() << "当前播放进度ms：" << pos;
        int content = pos;
        content_pos = content / 1000;  // ms 转 秒
        //qDebug() << "当前播放进度s：" << content_pos;
        ui->horizontalSlider_music->setValue(pos);// 更新滑块位置
    });
        
    ui->horizontalSlider_music->setStyleSheet(
        // 滑块容器设置
        "QSlider {"
        "    min-width: 220px;"
        "    max-width: 220px;"
        "    min-height: 12px;"  // 容器最小高度
        "}"
        
        // 轨道样式（极细3px）- 主背景层
        "QSlider::groove:horizontal {"
        "    border: 1px solid #d9d9d9;"
        "    background: white;"
        "    height: 3px;"
        "    border-radius: 1.5px;"
        "    margin: 4px 0;"
        "}"
        
        // 进度条（蓝紫渐变）- 严格匹配轨道尺寸
        "QSlider::sub-page:horizontal {"
        "    background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "                               stop:0 #8a2be2, stop:1 #00d4ff);"  // 紫到荧光蓝
        "    height: 3px;"       // 严格匹配轨道高度
        "    margin-top: 4px;"   // 与轨道margin同步
        "    margin-bottom: 4px;"
        "    border-radius: 1.5px;"
        "}"
        
        // 未滑过部分（透明且不干扰布局）
        "QSlider::add-page:horizontal {"
        "    background: transparent;"
        "    height: 0px;"       // 避免产生额外空间
        "    margin: 0;"
        "}"
        
        // 手柄（8px圆形，精确垂直居中）
        "QSlider::handle:horizontal {"
        "    background: qlineargradient(x1:0, y1:0, x2:1, y2:1,"
        "                               stop:0 #2ecc71, stop:1 #27ae60);"
        "    border: 1px solid #27ae60;"
        "    width: 8px;"
        "    height: 8px;"
        "    margin: -3px 0;"    // 计算公式：(3+4+4-8)/2 = -3
        "    border-radius: 4px;"
        "}"
        
        // 悬停状态（手柄轻微放大）
        "QSlider::handle:horizontal:hover {"
        "    background: qlineargradient(x1:0, y1:0, x2:1, y2:1,"
        "                               stop:0 #5efc82, stop:1 #2ecc71);"
        "    width: 10px;"
        "    height: 10px;"
        "    margin: -4px 0;"     // 重新计算居中：(3+4+4-10)/2 = -4
        "}"
    );
    
    // 按钮图标
    ui->pushButton_music_tp_1->setIcon(QIcon(":/音乐音箱"));// 音乐音箱
    ui->pushButton_music_tp_1->setText("");
    ui->pushButton_music_tp_1->setIconSize(QSize(80, 50));
    ui->pushButton_music_tp_1->setFlat(true);  // 移除默认背景（可选，但可能影响 hover 效果）
    ui->pushButton_music_tp_1->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    );
    
    ui->pushButton_music_tp_2->setIcon(QIcon(":/音乐下一首_关"));// 音乐下一首
    ui->pushButton_music_tp_2->setText("");
    ui->pushButton_music_tp_2->setIconSize(QSize(40, 30));
    ui->pushButton_music_tp_2->setFlat(true);  // 移除默认背景（可选，但可能影响 hover 效果）
    ui->pushButton_music_tp_2->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    );
    
    ui->pushButton_music_tp_3->setIcon(QIcon(":/音乐播放_关"));// 音乐播放
    ui->pushButton_music_tp_3->setText("");
    ui->pushButton_music_tp_3->setIconSize(QSize(40, 30));
    ui->pushButton_music_tp_3->setFlat(true);  // 移除默认背景（可选，但可能影响 hover 效果）
    ui->pushButton_music_tp_3->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    );
    
    ui->pushButton_music_tp_4->setIcon(QIcon(":/音乐上一首_关"));// 音乐上一首
    ui->pushButton_music_tp_4->setText("");
    ui->pushButton_music_tp_4->setIconSize(QSize(40, 30));
    ui->pushButton_music_tp_4->setFlat(true);  // 移除默认背景（可选，但可能影响 hover 效果）
    ui->pushButton_music_tp_4->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    );
    
    
    
    // 视频播放
    // 创建媒体播放器和视频显示部件
    mediaPlayer = new QMediaPlayer(this);
    
    // 当播放结束时重新播放
    connect(mediaPlayer, &QMediaPlayer::mediaStatusChanged, [=](QMediaPlayer::MediaStatus status) {
        if (status == QMediaPlayer::EndOfMedia) {
            mediaPlayer->setPosition(0);  // 回到开头
            mediaPlayer->play();          // 重新播放
        }
    });
    
    // 创建图形场景和视图
    QGraphicsScene *scene = new QGraphicsScene(this);
    QGraphicsVideoItem *videoItem = new QGraphicsVideoItem;
    scene->addItem(videoItem);
    
    // 设置视频输出到QVideoWidget
    mediaPlayer->setVideoOutput(videoItem);
    
    // 创建图形视图并设置场景
    QGraphicsView *graphicsView = new QGraphicsView(this);
    graphicsView->setScene(scene);
    graphicsView->setGeometry(0,6,400,200);
    
    // 设置无边框
    graphicsView->setFrameShape(QFrame::NoFrame);
    graphicsView->setStyleSheet("background: transparent; border: 0px;");
    
    // 控制视频窗口属性
    graphicsView->setBackgroundBrush(Qt::NoBrush);        // 透明背景
    graphicsView->setRenderHint(QPainter::Antialiasing);  // 抗锯齿
    graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);  // 隐藏水平滚动条
    graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);    // 隐藏垂直滚动条
    
    // 调整视频项尺寸适应视图
    videoItem->setSize(graphicsView->size());
    
    // 使用布局管理器将 videoWidget 添加到主窗口
    QVBoxLayout *layout = new QVBoxLayout(ui->label);
    layout->setContentsMargins(0, 0, 0, 0);  // 移除边距，让视频填满整个 label
    layout->addWidget(graphicsView);
    ui->label->setLayout(layout);    // 应用布局        
    
    
    // 摄像头    
    // 创建 QCamera 和 QCameraViewfinder
    camera = new QCamera(this);
    viewfinder = new QCameraViewfinder(this);
    imageCapture = new QCameraImageCapture(camera);

    // 将 QCamera 的视频输出设置到 QCameraViewfinder
    camera->setViewfinder(viewfinder);    
    viewfinder->setGeometry(466, 113, 220, 170);

    ui->pushButton_camera->setIcon(QIcon(":/视频聊天_关"));// 视频聊天
    ui->pushButton_camera->setText("");
    ui->pushButton_camera->setIconSize(QSize(240, 30));
    ui->pushButton_camera->setFlat(true);  // 移除默认背景
    ui->pushButton_camera->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    );            
      
    // 按钮图标
    ui->pushButton_openfile->setIcon(QIcon(":/文件_关"));// 文件
    ui->pushButton_openfile->setText("");
    ui->pushButton_openfile->setIconSize(QSize(30, 60));
    ui->pushButton_openfile->setFlat(true);  // 移除默认背景（可选，但可能影响 hover 效果）
    ui->pushButton_openfile->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    );
    
    ui->pushButton_play->setIcon(QIcon(":/播放_关"));// 播放
    ui->pushButton_play->setText("");
    ui->pushButton_play->setIconSize(QSize(30, 60));
    ui->pushButton_play->setFlat(true);  // 移除默认背景（可选，但可能影响 hover 效果）
    ui->pushButton_play->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    );
    
    ui->pushButton_suspend->setIcon(QIcon(":/暂停_关"));// 暂停
    ui->pushButton_suspend->setText("");
    ui->pushButton_suspend->setIconSize(QSize(30, 60));
    ui->pushButton_suspend->setFlat(true);  // 移除默认背景（可选，但可能影响 hover 效果）
    ui->pushButton_suspend->setStyleSheet(
        "QPushButton {"
        "   background: transparent;"
        "   border: none;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(200, 200, 200, 50);"  // 悬停效果
        "}"
    );
    
    // 归家 连接信号和槽
    connect(ui->label_return_home, &Button_home_start::signals_start, [=](bool start) {
        if (start) {
            ui->label_return_home_tp_1->setPixmap(QPixmap(":/灯_开"));
            ui->label_return_home_tp_2->setPixmap(QPixmap(":/空调_开"));
            ui->label_return_home_tp_3->setPixmap(QPixmap(":/回家_回"));
            
            ui->label_run_home_tp_1->setPixmap(QPixmap(":/扫地机_关"));
            ui->label_run_home_tp_2->setPixmap(QPixmap(":/窗帘_关"));
            ui->label_run_home_tp_3->setPixmap(QPixmap(":/离家_未"));
        } else {
            ui->label_return_home_tp_1->setPixmap(QPixmap(":/灯_关"));
            ui->label_return_home_tp_2->setPixmap(QPixmap(":/空调_关"));
            ui->label_return_home_tp_3->setPixmap(QPixmap(":/回家_未"));
            
            ui->label_run_home_tp_1->setPixmap(QPixmap(":/扫地机_开"));
            ui->label_run_home_tp_2->setPixmap(QPixmap(":/窗帘_开"));
            ui->label_run_home_tp_3->setPixmap(QPixmap(":/离家_离"));
        }
    });

    // 离家 连接信号和槽
    connect(ui->label_run_home, &Button_home_start::signals_start, [=](bool start) {
        if (start) {
            ui->label_run_home_tp_1->setPixmap(QPixmap(":/扫地机_开"));
            ui->label_run_home_tp_2->setPixmap(QPixmap(":/窗帘_开"));
            ui->label_run_home_tp_3->setPixmap(QPixmap(":/离家_离"));
            
            ui->label_return_home_tp_1->setPixmap(QPixmap(":/灯_关"));
            ui->label_return_home_tp_2->setPixmap(QPixmap(":/空调_关"));
            ui->label_return_home_tp_3->setPixmap(QPixmap(":/回家_未"));
            
        } else {            
            ui->label_run_home_tp_1->setPixmap(QPixmap(":/扫地机_关"));
            ui->label_run_home_tp_2->setPixmap(QPixmap(":/窗帘_关"));
            ui->label_run_home_tp_3->setPixmap(QPixmap(":/离家_未"));
            
            ui->label_return_home_tp_1->setPixmap(QPixmap(":/灯_开"));
            ui->label_return_home_tp_2->setPixmap(QPixmap(":/空调_开"));
            ui->label_return_home_tp_3->setPixmap(QPixmap(":/回家_回"));
        }
    });
    
    // 起床 连接信号和槽
    connect(ui->label_get_up, &Button_home_start::signals_start, [=](bool start) {
        if (start) {
            ui->label_sleep->set_start(false);
            ui->label_get_up_tp_1->setPixmap(QPixmap(":/灯_开"));
            ui->label_get_up_tp_2->setPixmap(QPixmap(":/窗帘_开"));
            
            ui->label_sleep_tp_1->setPixmap(QPixmap(":/灯_开"));
            ui->label_sleep_tp_2->setPixmap(QPixmap(":/窗帘_开"));           
        } else {            
            ui->label_get_up_tp_1->setPixmap(QPixmap(":/灯_关"));
            ui->label_get_up_tp_2->setPixmap(QPixmap(":/窗帘_关"));
            
            ui->label_sleep_tp_1->setPixmap(QPixmap(":/灯_关"));
            ui->label_sleep_tp_2->setPixmap(QPixmap(":/窗帘_关")); 
        }
    });
    
    // 睡觉 连接信号和槽
    connect(ui->label_sleep, &Button_home_start::signals_start, [=](bool start) {
        if (start) {
            ui->label_get_up->set_start(false);
            ui->label_sleep_tp_1->setPixmap(QPixmap(":/灯_关"));
            ui->label_sleep_tp_2->setPixmap(QPixmap(":/窗帘_关")); 
            
            ui->label_get_up_tp_1->setPixmap(QPixmap(":/灯_关"));
            ui->label_get_up_tp_2->setPixmap(QPixmap(":/窗帘_关"));
        } else {            
            ui->label_sleep_tp_1->setPixmap(QPixmap(":/灯_开"));
            ui->label_sleep_tp_2->setPixmap(QPixmap(":/窗帘_开"));
            
            ui->label_get_up_tp_1->setPixmap(QPixmap(":/灯_开"));
            ui->label_get_up_tp_2->setPixmap(QPixmap(":/窗帘_开"));
        }
    });    
    
    ui->label_return_home_2->setGeometry(10,10,40,20);
    ui->label_return_home_2->setText("归家");
    ui->label_return_home_2->setFont(QFont("楷体" , 15));
    ui->label_return_home_2->setStyleSheet("color: #e056c4;");
    
    ui->label_run_home_2->setGeometry(10,10,40,20);
    ui->label_run_home_2->setText("离家");
    ui->label_run_home_2->setFont(QFont("楷体" , 15));
    ui->label_run_home_2->setStyleSheet("color: #e056c4;");
    
    ui->label_get_up_2->setGeometry(10,10,40,20);
    ui->label_get_up_2->setText("起床");
    ui->label_get_up_2->setFont(QFont("楷体" , 15));
    ui->label_get_up_2->setStyleSheet("color: #e056c4;");
    
    ui->label_sleep_2->setGeometry(10,10,40,20);
    ui->label_sleep_2->setText("睡觉");
    ui->label_sleep_2->setFont(QFont("楷体" , 15));
    ui->label_sleep_2->setStyleSheet("color: #e056c4;");
    
    ui->label_date_time->setGeometry(10,10,40,20);
    ui->label_date_time->setText("时间");
    ui->label_date_time->setFont(QFont("楷体" , 15));
    ui->label_date_time->setStyleSheet("color: #e056c4;");
    
    ui->label_date_time_2->setFont(QFont("楷体" , 15));
    ui->label_date_time_2->setStyleSheet("color: #e056c4;");
    
    QTimer *timer = new QTimer(this);
    
    // 连接定时器信号
    connect(timer, &QTimer::timeout, [=](){
        ui->label_date_time_2->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd\n hh:mm:ss"));
    });
    
    timer->start(1000); // 每秒更新一次   
    
    // 扫地机 连接信号和槽
    connect(ui->label_sweeper, &Button_home_start::signals_start, [=](bool start) {
        if (start) {
            ui->label_sweeper_1->setText("ON");
            ui->label_sweeper_tp_1->setPixmap(QPixmap(":/扫地机_开"));
        } else {   
            ui->label_sweeper_1->setText("OFF");
            ui->label_sweeper_tp_1->setPixmap(QPixmap(":/扫地机_关"));
        }
    });
    
    // 路由器 连接信号和槽
    connect(ui->label_Router, &Button_home_start::signals_start, [=](bool start) {
        if (start) {
            ui->label_Router_1->setText("ON");
            ui->label_Router_tp_1->setPixmap(QPixmap(":/路由器_开"));
        } else {          
            ui->label_Router_1->setText("OFF");
            ui->label_Router_tp_1->setPixmap(QPixmap(":/路由器_关"));
        }
    });
    
    // 窗帘 连接信号和槽
    connect(ui->label_curtain, &Button_home_start::signals_start, [=](bool start) {
        if (start) {
            ui->label_curtain_1->setText("ON");
            ui->label_curtain_tp_1->setPixmap(QPixmap(":/窗帘_开"));
        } else {            
            ui->label_curtain_1->setText("OFF");
            ui->label_curtain_tp_1->setPixmap(QPixmap(":/窗帘_关")); 
        }
    });

    // 冰箱 连接信号和槽
    connect(ui->label_fridge, &Button_home_start::signals_start, [=](bool start) {
        if (start) {
            ui->label_fridge_1->setText("ON");
            ui->label_fridge_tp_1->setPixmap(QPixmap(":/冰箱_开"));
        } else {   
            ui->label_fridge_1->setText("OFF");
            ui->label_fridge_tp_1->setPixmap(QPixmap(":/冰箱_关"));
        }
    });
    
    // 空调 连接信号和槽
    connect(ui->label_air_conditioner, &Button_home_start::signals_start, [=](bool start) {
        if (start) {
            ui->label_air_conditioner_1->setText("ON");
            ui->label_air_conditioner_tp_1->setPixmap(QPixmap(":/空调_开"));
        } else {          
            ui->label_air_conditioner_1->setText("OFF");
            ui->label_air_conditioner_tp_1->setPixmap(QPixmap(":/空调_关"));
        }
    });
    
    // 灯 连接信号和槽
    connect(ui->label_light, &Button_home_start::signals_start, [=](bool start) {
        if (start) {
            ui->label_light_1->setText("ON");
            ui->label_light_tp_1->setPixmap(QPixmap(":/灯_开"));
        } else {            
            ui->label_light_1->setText("OFF");
            ui->label_light_tp_1->setPixmap(QPixmap(":/灯_关")); 
        }
    });
    ui->label_sweeper_1->setGeometry(10,10,40,20);
    ui->label_sweeper_1->setText("OFF");
    ui->label_sweeper_1->setFont(QFont("楷体" , 15));
    ui->label_sweeper_1->setStyleSheet("color: #e056c4;");
    
    ui->label_sweeper_2->setGeometry(10,60,60,20);
    ui->label_sweeper_2->setText("扫地机");
    ui->label_sweeper_2->setFont(QFont("楷体" , 15));
    ui->label_sweeper_2->setStyleSheet("color: #e056c4;");
    
    ui->label_Router_1->setGeometry(10,10,40,20);
    ui->label_Router_1->setText("OFF");
    ui->label_Router_1->setFont(QFont("楷体" , 15));
    ui->label_Router_1->setStyleSheet("color: #e056c4;");
    
    ui->label_Router_2->setGeometry(10,60,60,20);
    ui->label_Router_2->setText("路由器");
    ui->label_Router_2->setFont(QFont("楷体" , 15));
    ui->label_Router_2->setStyleSheet("color: #e056c4;");
    
    ui->label_curtain_1->setGeometry(10,10,40,20);
    ui->label_curtain_1->setText("OFF");
    ui->label_curtain_1->setFont(QFont("楷体" , 15));
    ui->label_curtain_1->setStyleSheet("color: #e056c4;");
    
    ui->label_curtain_2->setGeometry(10,60,60,20);
    ui->label_curtain_2->setText("窗帘");
    ui->label_curtain_2->setFont(QFont("楷体" , 15));
    ui->label_curtain_2->setStyleSheet("color: #e056c4;");
    
    ui->label_fridge_1->setGeometry(10,10,40,20);
    ui->label_fridge_1->setText("OFF");
    ui->label_fridge_1->setFont(QFont("楷体" , 15));
    ui->label_fridge_1->setStyleSheet("color: #e056c4;");
    
    ui->label_fridge_2->setGeometry(10,60,60,20);
    ui->label_fridge_2->setText("冰箱");
    ui->label_fridge_2->setFont(QFont("楷体" , 15));
    ui->label_fridge_2->setStyleSheet("color: #e056c4;");
    
    ui->label_air_conditioner_1->setGeometry(10,10,40,20);
    ui->label_air_conditioner_1->setText("OFF");
    ui->label_air_conditioner_1->setFont(QFont("楷体" , 15));
    ui->label_air_conditioner_1->setStyleSheet("color: #e056c4;");
    
    ui->label_air_conditioner_2->setGeometry(10,60,60,20);
    ui->label_air_conditioner_2->setText("空调");
    ui->label_air_conditioner_2->setFont(QFont("楷体" , 15));
    ui->label_air_conditioner_2->setStyleSheet("color: #e056c4;");
    
    ui->label_light_1->setGeometry(10,10,40,20);
    ui->label_light_1->setText("OFF");
    ui->label_light_1->setFont(QFont("楷体" , 15));
    ui->label_light_1->setStyleSheet("color: #e056c4;");
    
    ui->label_light_2->setGeometry(10,60,60,20);
    ui->label_light_2->setText("灯");
    ui->label_light_2->setFont(QFont("楷体" , 15));
    ui->label_light_2->setStyleSheet("color: #e056c4;");
    
    // 按钮
    ui->label_get_up->setPixmap(QPixmap(":/状态按钮_关"));
    ui->label_sleep->setPixmap(QPixmap(":/状态按钮_关"));
    ui->label_sweeper->setPixmap(QPixmap(":/状态按钮_关"));    
    ui->label_Router->setPixmap(QPixmap(":/状态按钮_关"));
    ui->label_curtain->setPixmap(QPixmap(":/状态按钮_关"));
    ui->label_fridge->setPixmap(QPixmap(":/状态按钮_关"));    
    ui->label_air_conditioner->setPixmap(QPixmap(":/状态按钮_关"));
    ui->label_light->setPixmap(QPixmap(":/状态按钮_关"));
    
    ui->label_return_home->setPixmap(QPixmap(":/状态按钮_关"));
    ui->label_run_home->setPixmap(QPixmap(":/状态按钮_关"));
    
    // 图标
    ui->label_return_home_tp_1->setPixmap(QPixmap(":/灯_关"));
    ui->label_return_home_tp_2->setPixmap(QPixmap(":/空调_关"));
    ui->label_return_home_tp_3->setPixmap(QPixmap(":/回家_未"));
    
    ui->label_run_home_tp_1->setPixmap(QPixmap(":/扫地机_关"));
    ui->label_run_home_tp_2->setPixmap(QPixmap(":/窗帘_关"));
    ui->label_run_home_tp_3->setPixmap(QPixmap(":/离家_未"));
    
    ui->label_get_up_tp_1->setPixmap(QPixmap(":/灯_关"));
    ui->label_get_up_tp_2->setPixmap(QPixmap(":/窗帘_关"));
    
    ui->label_sleep_tp_1->setPixmap(QPixmap(":/灯_关"));
    ui->label_sleep_tp_2->setPixmap(QPixmap(":/窗帘_关"));
    
    ui->label_sweeper_tp_1->setPixmap(QPixmap(":/扫地机_关"));
    
    ui->label_Router_tp_1->setPixmap(QPixmap(":/路由器_关"));
    
    ui->label_curtain_tp_1->setPixmap(QPixmap(":/窗帘_关"));   
    
    ui->label_fridge_tp_1->setPixmap(QPixmap(":/冰箱_关"));
    
    ui->label_air_conditioner_tp_1->setPixmap(QPixmap(":/空调_关"));
    
    ui->label_light_tp_1->setPixmap(QPixmap(":/灯_关"));
    
    ui->label_date_time_tp_1->setPixmap(QPixmap(":/日期_时间"));
       
    
    // 连接按钮信号
    connect(ui->label_return_home , &Button_home_start::signals_start , this , &Widget_funcC_Smart_Home::slots_button_start);
    connect(ui->label_run_home , &Button_home_start::signals_start , this , &Widget_funcC_Smart_Home::slots_button_start);
}

Widget_funcC_Smart_Home::~Widget_funcC_Smart_Home()
{
    delete ui;
}



void Widget_funcC_Smart_Home::slots_button_start(bool start)
{
    
    QObject * temp = sender();
    // 离开
    if(temp == ui->label_run_home)
    {
        // 离家开
        ui->label_run_home->set_start(start);
        // 归家关
        ui->label_return_home->set_start(!start);
    }
    // 归家
    if(temp == ui->label_return_home)
    {
        // 离家开
        ui->label_return_home->set_start(start);
        // 归家关
        ui->label_run_home->set_start(!start);
    }
}

void Widget_funcC_Smart_Home::slots_button_send_message()
{
    // 增加一个widget  内部包含
    
}



void Widget_funcC_Smart_Home::on_pushButton_openfile_clicked()
{
    ui->pushButton_openfile->setIcon(QIcon(":/文件_开"));
    QString defaultPath = "D:\\CSESPX\\Qt\\Qt_UI\\MV";  // 自定义默认路径
    // 获取视频文件路径
    QString fileName = QFileDialog::getOpenFileName(this,
        tr("打开视频文件"),
        defaultPath,
        tr("视频文件 (*.mp4 *.avi *.mkv *.mov)"));
    
    if (!fileName.isEmpty()) {
        // 设置媒体内容
        mediaPlayer->setMedia(QUrl::fromLocalFile(fileName));
        qDebug()<<"已加载: " + fileName <<endl;
    }
}

void Widget_funcC_Smart_Home::on_pushButton_play_clicked()
{    
    ui->pushButton_openfile->setIcon(QIcon(":/文件_关"));
    ui->pushButton_suspend->setIcon(QIcon(":/暂停_关"));
    ui->pushButton_play->setIcon(QIcon(":/播放_开"));
    mediaPlayer->play();
    //qDebug()<<"视频播放中..."<<endl;
}

void Widget_funcC_Smart_Home::on_pushButton_suspend_clicked()
{
    ui->pushButton_openfile->setIcon(QIcon(":/文件_关"));
    ui->pushButton_play->setIcon(QIcon(":/播放_关"));
    ui->pushButton_suspend->setIcon(QIcon(":/暂停_开"));
    mediaPlayer->pause();
    //qDebug()<<"视频已暂停"<<endl;
}

void Widget_funcC_Smart_Home::togglecamera()  // 启动或停止 摄像头
{
    if (camera->state() == QCamera::ActiveState) { //QCamera::ActiveState - 摄像头已激活并运行
        camera->stop();  // 停止摄像头
        ui->pushButton_camera->setIcon(QIcon(":/视频聊天_关"));       
        viewfinder->hide(); // 隐藏视图
    } else {           
        camera->start(); // 开始摄像头
        ui->pushButton_camera->setIcon(QIcon(":/视频聊天_开"));            
        viewfinder->show(); // 显示视图
    }
} 

int i=0;
void Widget_funcC_Smart_Home::on_pushButton_music_tp_3_clicked()
{
    i++;
         
    if(player->state()==QMediaPlayer::PlayingState)//  正在播放
    {
        i=2;
    }   
    else if(player->state()==QMediaPlayer::StoppedState)//  播放器已停止（未播放）
    {
        i=1;
    }
    
    if(i==1)
    {
        if(player->state()==QMediaPlayer::StoppedState)//  播放器已停止（未播放）
        {
            ui->pushButton_music_tp_4->setIcon(QIcon(":/音乐上一首_开"));// 音乐播放上一首
            ui->pushButton_music_tp_2->setIcon(QIcon(":/音乐下一首_开"));// 音乐播放下一首
            
            ui->pushButton_music_tp_3->setIcon(QIcon(":/音乐播放_开"));// 音乐播放
            
            movie->start(); // GIF开始播放                                                       
            
            player->play(); // 音乐开始播放​            
              
            player->setVolume(100); // 设置音量​
            
            // 获取播放进度的正确位置
            //qDebug() << "初始播放进度：" << player->position();
            //qDebug() << "播放状态1：" << player->state();                       
        }        
    }    
    else if(i==2)
    {
        if(player->state()==QMediaPlayer::PlayingState) // 正在播放
        {
            ui->pushButton_music_tp_4->setIcon(QIcon(":/音乐上一首_开"));// 音乐播放上一首
            ui->pushButton_music_tp_2->setIcon(QIcon(":/音乐下一首_开"));// 音乐播放下一首
            //movie->stop();// 停止播放
            movie->setPaused(true);  // 暂停动画
            ui->pushButton_music_tp_3->setIcon(QIcon(":/音乐播放_关")); // 暂停音乐播放
            
            player->pause();  // 暂停播放
            // 获取暂停时的播放进度
            //qDebug() << "暂停时进度：" << player->position();
            //qDebug() << "播放状态2：" << player->state();           
        }
    }    
    else if(i==3)
    {
        if(player->state()==QMediaPlayer::PausedState)//  已暂停
        {
            ui->pushButton_music_tp_4->setIcon(QIcon(":/音乐上一首_开"));// 音乐播放上一首
            ui->pushButton_music_tp_2->setIcon(QIcon(":/音乐下一首_开"));// 音乐播放下一首
            
            ui->pushButton_music_tp_3->setIcon(QIcon(":/音乐播放_开"));// 音乐播放
            
            movie->setPaused(false); // 恢复播放
                        
            player->setPosition(player->position()); // 暂停位置播放
            
            player->play(); // 音乐开始播放
            //qDebug() << "暂停时进度：" << player->position();
            //qDebug() << "播放状态3：" << player->state();            
        }
        i=0;
    }
}

void Widget_funcC_Smart_Home::on_pushButton_music_tp_1_clicked()
{
    QString defaultPath = "D:\\CSESPX\\Qt\\Qt_UI\\json";  // 自定义默认路径
    QString fileName = QFileDialog::getOpenFileName(this,
                                                    tr("打开文本文件"),
                                                    defaultPath,
                                                    tr("文本文件 (*.txt)"));
    
    if (!fileName.isEmpty()) 
    {
        qDebug() << "已选择文件: " << fileName;
        
        // 获取文件名（不含路径）
        QFileInfo fileInfo(fileName);
        QString baseName = fileInfo.baseName();  // 获取不带扩展名的文件名
        
        // 检查是否包含 "-" 分隔符
        if (baseName.contains('-')) 
        {
            // 分割文件名
            QStringList parts = baseName.split('-');
            
            // 打印分割后的部分
            ui->label_music_song_name->setText( parts[0].trimmed());  // 去除前后空格 "- 前半部分:"
            if (parts.size() > 1) {
                ui->label_music_singer->setText( parts[1].trimmed()); // "- 后半部分:"
            }
            
            if(parts[0].trimmed()=="喜欢你")
            {
                player->setMedia(QUrl(str1)); 
                num=1;
            }
            
            else if(parts[0].trimmed()=="我们是光荣的武警部队")
            {
                player->setMedia(QUrl(str2)); 
                num=2;
            }
            
            else if(parts[0].trimmed()=="军中绿花")
            {
                player->setMedia(QUrl(str3)); 
                num=3;
            }            
            else if(parts[0].trimmed()=="中国人民解放军军歌")
            {
                player->setMedia(QUrl(str4)); 
                num=4;
            }
        } else {
            qDebug() << "文件名不包含 '-' 分隔符";
        }
        
        if (!fileName.isEmpty()) {
            // 读取文件
            QFile file(fileName);
            if (file.open(QIODevice::ReadWrite | QIODevice::Text)) {
                QTextStream in(&file);
                content = in.readAll();  // 读取全部内容
                //qDebug() << content <<endl;
            }
        }
    }
}

void Widget_funcC_Smart_Home::RegularExpression()
{
    // 定义正则表达式：匹配 "数字+ms" 后的内容，直到换行符
    QRegularExpression regex(R"((\d+)ms\s([^\n]+))");
    QRegularExpressionMatchIterator it = regex.globalMatch(content);
    
    while (it.hasNext()) {
        QRegularExpressionMatch match = it.next();
        int ms = match.captured(1).toInt();       // 提取时间（如 18850）
        int seconds = ms / 1000;  // ms 转 秒
        if (content_pos==seconds)
        {
            QString ms_content = match.captured(2);      // 提取 "ms" 后的内容
            ui->label_music_lyric->setText( ms_content);
            //qDebug() << ms << "ms:" << ms_content;
        }
        
    }
}

void Widget_funcC_Smart_Home::gc_list()
{
    QString fileName;
    switch (num) {
    case 1:
        fileName = fileName1;
        break;
    case 2:
        fileName = fileName2;
        break;
    case 3:
        fileName = fileName3;
        break;
    case 4:
        fileName = fileName4;
        break;
    }
    
    if (!fileName.isEmpty()) 
    {
        qDebug() << "已选择文件: " << fileName;
        
        // 获取文件名（不含路径）
        QFileInfo fileInfo(fileName);
        QString baseName = fileInfo.baseName();  // 获取不带扩展名的文件名
        
        // 检查是否包含 "-" 分隔符
        if (baseName.contains('-')) 
        {
            // 分割文件名
            QStringList parts = baseName.split('-');
            
            // 打印分割后的部分
            ui->label_music_song_name->setText( parts[0].trimmed());  // 去除前后空格 "- 前半部分:"
            if (parts.size() > 1) {
                ui->label_music_singer->setText( parts[1].trimmed()); // "- 后半部分:"
            }
        } else {
            qDebug() << "文件名不包含 '-' 分隔符";
        }
        
        if (!fileName.isEmpty()) {
            // 读取文件
            QFile file(fileName);
            if (file.open(QIODevice::ReadWrite | QIODevice::Text)) {
                QTextStream in(&file);
                content = in.readAll();  // 读取全部内容
                //qDebug() << content <<endl;
            }
        }
    }
}
void Widget_funcC_Smart_Home::on_pushButton_music_tp_2_clicked()
{
    ui->pushButton_music_tp_2->setIcon(QIcon(":/音乐下一首_开"));// 音乐播放下一首
    ui->pushButton_music_tp_4->setIcon(QIcon(":/音乐上一首_关"));// 音乐播放上一首
    if(num>=5){
        num=1;
    }else num+=1;
    gc_list();
    
    switch (num) {
    case 1:
        player->setMedia(QUrl(str1));
        break;
    case 2:
        player->setMedia(QUrl(str2));
        break;
    case 3:
        player->setMedia(QUrl(str3));
        break;
    case 4:
        player->setMedia(QUrl(str4));
        break;
    }
}

void Widget_funcC_Smart_Home::on_pushButton_music_tp_4_clicked()
{
    ui->pushButton_music_tp_4->setIcon(QIcon(":/音乐上一首_开"));// 音乐播放上一首
    ui->pushButton_music_tp_2->setIcon(QIcon(":/音乐下一首_关"));// 音乐播放下一首
    if(num<=0){
        num=4;
    }else num -= 1;
    gc_list();
    
    switch (num) {
    case 1:
        player->setMedia(QUrl(str1));
        break;
    case 2:
        player->setMedia(QUrl(str2));
        break;
    case 3:
        player->setMedia(QUrl(str3));
        break;
    case 4:
        player->setMedia(QUrl(str4));
        break;
    }
}
